#pragma once
#include <iostream>
using namespace std;

class Date
{
public:
	Date(int year = 2000, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	bool operator <(const Date& x);
	bool operator ==(const Date& x);
	bool operator >(const Date& x);
	bool operator <=(const Date& x);
	bool operator >=(const Date& x);
	bool operator !=(const Date& x);

	int GetMonthDay(int year,int month);//获取月份的天数
	Date operator +=(int day);//n天后的日期(往前算给负数即可)

	Date operator +(int day);//与上式相比，不会改变原来的数本身（被加数）

	void Print()
	{
		cout << "日期为：" << _year << "年" << _month << "月" << _day << "日" << endl;
	}
	
private:
	int _year;
	int _month;
	int _day;
};

bool Date::operator <(const Date& x)
{
	if (_year < x._year)
		return true;
	else if (_year == x._year && _month < x._month)
		return true;
	else if (_year == x._year && _month == x._month && _day < x._day)
		return true;
	return false;
}

bool Date::operator ==(const Date& x)
{
	return _year == x._year && _month == x._month && _day == x._day;
}

bool Date::operator >(const Date& x)
{
	return !(*this <= x);
}
bool Date::operator <=(const Date& x)
{
	return (*this < x) || (*this == x);
}

bool Date::operator >=(const Date& x)
{
	return !(*this < x);
}

bool Date::operator !=(const Date& x)
{
	return !(*this == x);
}


int Date::GetMonthDay(int year, int month)//判断一个月有多少天
{
	static int daysArr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	//设置为静态时会在静态区创建，由于函数会频繁调用所以不必每次调用都开辟一个数组的空间
	if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
		return 29;
	return daysArr[month];
}

Date Date::operator +(int day)//不会改变原来的对象
{
	Date tmp(*this);//拷贝构造

	tmp._day += day;
	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);
		tmp._month++;
		if (tmp._month == 13)
		{
			tmp._month = 1;
			tmp._year++;
		}
	}
	return tmp;
}

Date Date::operator +=(int day)//会改变原来的对象
{
	_day += day;
	while (_day > GetMonthDay(_year,_month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

//可以复用：

Date Date::operator +(int day)
{
    Date tmp(*this);
    tmp += day;
    return tmp;
}
//or
Date Date::operator +=(int day)
{
    *this = *this + day;
    return *this;
}
